<?php
/**
 * 
 * Enter description here ...
 * @author Xinzhao Liang
 *
 */
class User_model extends CI_Model {

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}
	
		

	/**
	 * 
	 * insert a new user
	 * @param  $username
	 * @param  $realname
	 * @param  $password
	 * @param  $profile
	 * @return true or false;
	 */
	function add_new_user($username = NULL,$realname = NULL,$password = NULL,$profile=NULL)
	{
		
		$sql = "INSERT INTO user (u_name,u_username,u_pwd,u_profile) VALUES ";
		$sql .= " ('" . $username . "','" .$realname. "','" . $password . "','" . $profile."')"; 
		$result = $this->db->query($sql);
 		return $result;
	}

	/**
	*
	* get amount of user
	* @param  $amount, default 10;
	* @param  $photo , along with the profile photo or not,default true
	* @return array,could be empty array:
	*/
	function get_user($amount = 10, $type = RANDOM ,$photo = true)
	{
		
		if($amount<=0){
			return array();
		}
		$select = " SELECT u_name,u_username,u_profile FROM user WHERE 1 ";
		
		if($type == RANDOM){
			$where = " AND RAND() <= ".RANDOM_PROPERTY; 
		}
		
		 
		$limit = " LIMIT $amount ";
		
		
		$sql = $select . $where . $limit;
		$q = $this->db->query($sql);
		$result = $q->result_array($q);
		if($result){
				foreach($result as $k=>$v){
					if($photo){
						$result[$k]['photo'] = get_profile_photo(); // a dummy photo to the profile photo
					}
					$result[$k]['u_profile'] = substr($result[$k]['u_profile'],0,20)."...";
				}		
			
			return $result;
		}else{
			return array();
		}
	}
	
	/**
	*
	* check whether the user pwd is correct
	* @param  $username
	* @param  $pwd
	* @return array(u_name,u_username,u_pwd)/false
	*/
	function chk_user($username = NULL,$pwd = NULL)
	{
		
		if(empty($username) || empty($pwd)){
			return false;
		}
		
		$sql = "SELECT u_name,u_username,u_pwd FROM user WHERE u_name = '". $username."'";
		$q = $this->db->query($sql);
		$result = $q->result_array($q);
		if(count($result) >0 ){
		   $r_pwd = $result[0]['u_pwd'];
 		   if($pwd == $r_pwd){//password is correct
			  return $result;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	/**
	 * send a friend request to other 
	 * @param sender 
	 * @param receiver
	 * @param content ,could be empty
	 * @return true/false
	 **/
	function send_friend_request($sender,$recv,$content = NULL)
	{
		if(empty($sender) || empty($recv))
		{
			return false;
		}else
		{
			$sql = "INSERT INTO friend ";
			$sql .= "(f_sender,f_receiver,f_content,f_response,send_time) VALUES";
			$sql .= " ('".$sender."','".$recv."','".$content."',2,".time().") ";
			//$sql .= " WHERE f_sender = '".$sender."' AND f_receiver = '".$recv."' ";
			//$sql .= " AND f_response IS NOT NULL"; //if a previous request exists and not responsed yet,
												   //resend is allow
												   //else you cant not send another one when other has made decision
			//echo $sql;
			$result = $this->db->query($sql);
			
			if($result){
				return true;
			}else{
				return false;
			}
		}
	}
	
	function get_search_result($search){
		$sql = "SELECT * FROM user WHERE u_username LIKE '%".$search."%'";
		$q = $this->db->query($sql);
		return $q->result_array($q);
	}
	
	function circle_users($username,$start = 0,$end = 20,$circle=null){
		$sql = " SELECT f_sender as username FROM friend WHERE 1 AND f_receiver = '".$username."' AND f_response = 1 ";
		if(!empty($circle)){
			$sql .= " AND f_type = '" .$circle."' ";
		}
		//$sql.=" LIMIT ".$start.",".$end;
		$q = $this->db->query($sql);
		$result = $q->result_array($q);
		return $result;
		
	}
}



/* End of file user_model.php */
/* Location: ./appliaction/models/user_model.php */